package com.test.template.publish.config.fac;

import com.elecbook.content.model.po.Contribute;
import com.test.template.publish.config.ControllerConfig;
import com.test.template.publish.config.JsClassConfig;
import com.test.template.publish.config.manager.VelocityConfigManager;
import com.test.template.publish.invoke.InvokeUtil;
import com.test.template.publish.invoke.PropComment;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class JsClassConfigFactory implements VelocityTemplateConfigFactory<JsClassConfig> {

    ConcurrentHashMap<String, JsClassConfig> configHashMap = new ConcurrentHashMap<>();

    // **单例定义
    // 私有静态变量保存唯一实例
    private static volatile JsClassConfigFactory instance = null;

    // **单例定义
    // 公共静态方法提供全局访问点
    public static JsClassConfigFactory getInstance() {
        if (instance == null) {
            synchronized (XmlSubmitConfigFactory.class) {
                if (instance == null) {
                    instance = new JsClassConfigFactory();
                    init();
                }
            }
        }
        return instance;
    }

    private static void init() {
        JsClassConfig contributeConfig = buildBasicConfig("contribute",
                Contribute.class);
        getInstance().addConfig("contribute", contributeConfig);
    }

    private static JsClassConfig buildBasicConfig(String tableName, Class<?> clazz) {
        JsClassConfig config = new JsClassConfig();
        config.setTableName(tableName);
        List<PropComment> props = InvokeUtil.getPropComments(clazz);
        config.setProps(props);
        config.innerGenerate();
        return config;
    }


    // 私有构造函数防止实例化
    private JsClassConfigFactory() {

    }


    @Override
    public JsClassConfig getConfig(String key) {
        return configHashMap.get(key);
    }

    @Override
    public void addConfig(String key, JsClassConfig config) {
        configHashMap.put(key, config);
    }

    @Override
    public void addConfig(VelocityConfigManager manager) {
        JsClassConfig config = buildBasicConfig(manager.getTableName(),manager.getClazzPo());
        getInstance().addConfig(manager.getTableName(), config);
    }

}
